# https://www.bilibili.com/v/anime/serial/?spm_id_from=333.334.b_7072696d6172795f6d656e75.8#/

# 动态加载 连载动画请求的url
# https://api.bilibili.com/x/web-interface/newlist?rid=33&type=0&pn=1&ps=20

# 下一页的地址
# https://api.bilibili.com/x/web-interface/newlist?rid=33&type=0&pn=2&ps=20
import requests
from lxml import etree
from Proiexs_pool import ProiexsPool
import json

class BiLi_spider(object):

    def __init__(self):
        self.url = 'https://api.bilibili.com/x/web-interface/newlist'
        self.html = ''
        self.headers = {
            'Referer': 'https://www.bilibili.com/v/anime/serial/?spm_id_from=333.334.b_7072696d6172795f6d656e75.8',
            'Host': 'api.bilibili.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36',
        }
        # for i in range(1,10):
        #     print(i)
        self.params = {
            'rid': 33,
            'type': 0,
            'pn': 1,
            'ps': 20,
        }

    def get_html(self):
        """
        获取源代码
        :return: response
        """
        # 代理IP
        proxies = ProiexsPool._get_random_ip()
        print(proxies)
        # 请求网页源码
        response = requests.get(url=self.url, headers=self.headers,params=self.params)
        # print(response.text)
        print(response.status_code)
        if response.status_code != 200:
            raise Exception('请求失败。。')
        # return response.text
        self.parse_html(response)

    def parse_html(self, response):
        """
        获取xpath
        :return:
        """

        comment = response.text
        # 转成json模式
        comment_dict = json.loads(comment)
        comments = comment_dict['data']["archives"]
        # print(comments)
        for data in comments:
            # print(data)
            title = data['title']  # 名称
            image = data['pic']  # 图片链接
            view = data['stat']['view']  # 播放量






    def run(self):
        self.get_html()


if __name__ == '__main__':
    bili = BiLi_spider()
    bili.run()